package net.xdclass.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import net.xdclass.enums.AddressStatusEnum;
import net.xdclass.interceptor.LoginInterceptor;
import net.xdclass.model.AddressDO;
import net.xdclass.mapper.AddressMapper;
import net.xdclass.model.LoginUser;
import net.xdclass.request.AddressAddReqeust;
import net.xdclass.service.AddressService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import net.xdclass.vo.AddressVO;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 电商-公司收发货地址表 服务实现类
 * </p>
 *
 * @author 二当家小D
 * @since 2022-01-07
 */
@Slf4j
@Service
public class AddressServiceImpl extends ServiceImpl<AddressMapper, AddressDO> implements AddressService {
    @Override
    public AddressVO detail(Long id) {
        LoginUser loginUser = LoginInterceptor.loginUserThreadLocal.get();
        AddressDO addressDO = this.getOne(new QueryWrapper<AddressDO>()
                .eq("id", id)
                .eq("user_id", loginUser.getId()));
        if (addressDO == null) {
            return null;
        }
        AddressVO addressVO = new AddressVO();
        BeanUtils.copyProperties(addressDO, addressVO);
        return addressVO;
    }

    @Override
    public void add(AddressAddReqeust addressAddReqeust) {
        LoginUser loginUser = LoginInterceptor.loginUserThreadLocal.get();
        AddressDO addressDO = new AddressDO();
        addressDO.setCreateTime(new Date());
        addressDO.setUserId(loginUser.getId());
        BeanUtils.copyProperties(addressAddReqeust, addressDO);

        if (addressDO.getDefaultStatus() == AddressStatusEnum.DEFAULT_SGTATUS.getStatus()) {
            AddressDO addressDO1 = baseMapper.selectOne(new QueryWrapper<AddressDO>().eq("user_id", addressDO.getUserId()).eq("default_status", AddressStatusEnum.DEFAULT_SGTATUS.getStatus()));
            if (addressDO1 != null) {
                addressDO1.setDefaultStatus(AddressStatusEnum.COMMON_STATUS.getStatus());
                baseMapper.updateById(addressDO1);
            }
        }

        int insert = baseMapper.insert(addressDO);
        log.info("新增收货地址:rows={},data={}", insert, addressDO);
    }

    @Override
    public int del(int addressId) {
        LoginUser loginUser = LoginInterceptor.loginUserThreadLocal.get();
        int id = baseMapper.delete(new QueryWrapper<AddressDO>()
                .eq("id", addressId)
                .eq("user_id", loginUser.getId()));
        return id;
    }

    @Override
    public List<AddressVO> listUserAllAddress() {
        LoginUser loginUser = LoginInterceptor.loginUserThreadLocal.get();
        List<AddressDO> list = baseMapper.selectList(new QueryWrapper<AddressDO>().eq("user_id", loginUser.getId()));
        List<AddressVO> collect = list.stream().map(obj -> {
            AddressVO addressVO = new AddressVO();
            BeanUtils.copyProperties(obj, addressVO);
            return addressVO;
        }).collect(Collectors.toList());
        return collect;
    }

}
